package com.Bigbluebook.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.Bigbluebook.R;
import com.Bigbluebook.entity.Comment;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.List;

public class MyCommentAdapter extends RecyclerView.Adapter<MyCommentAdapter.MyCommentViewHolder> {

    private List<Comment> commentList;

    public MyCommentAdapter(List<Comment> commentList) {
        this.commentList = commentList;
    }

    @NonNull
    @Override
    public MyCommentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.comment_item, parent, false);
        return new MyCommentViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MyCommentViewHolder holder, int position) {
        Comment comment = commentList.get(position);
        holder.commentContent.setText(comment.getContent());
        // 如果getCreatedAt()返回的是Date类型，需要转换为字符串
        Date createdAt = comment.getCreatedAt();
        if (createdAt != null) {
            // 定义日期格式
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            // 格式化日期
            String formattedDate = dateFormat.format(createdAt);
            holder.creationTime.setText(formattedDate);
        } else {
            holder.creationTime.setText("N/A"); // 或者其他默认文本
        }
    }

    @Override
    public int getItemCount() {
        if(commentList==null){
            return 0;
        }
        return commentList.size();
    }

    public void updateData(List<Comment> newCommentList) {
        commentList = newCommentList;
        notifyDataSetChanged();
    }

    public static class MyCommentViewHolder extends RecyclerView.ViewHolder {
        TextView commentContent;
        TextView creationTime;

        public MyCommentViewHolder(View itemView) {
            super(itemView);
            commentContent = itemView.findViewById(R.id.comment_Content);
            creationTime = itemView.findViewById(R.id.createComment_time);
        }
    }
}
